home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacHack 1997
/
MacHack 1997.toast
/
Hacks
/
Hacks ’97
/
Jasik Park - The Lost UI
/
JasikAppearance
/
JasikWIND.cpp
< prev
Wrap
C/C++ Source or Header
|
1997-06-28
|
7KB
|
267 lines
// *****************************************************************************
//
// InfinityWindoid.c
//
// —————————————————————————————————————————————————————————————————————————————
// Copyright © 1991-94 Infinity Systems. All rights reserved.
// —————————————————————————————————————————————————————————————————————————————
// DESCRIPTION:
// This file contains the main source for a WDEF (Window Definition)
// resource. It provides a ‘windoid’ appearance for use on floating windows.
//
// See the file ‘About Infinity Windoid’ for more information and a list
// of features this WDEF supports.
// —————————————————————————————————————————————————————————————————————————————
// WRITTEN BY:
// Troy Gaul, Infinity Systems
//
// HOW TO CONTACT THE AUTHOR:
// Send e-mail to: tgaul@halcyon.com
// or: tgaul@aol.com
// or: tgaul@eworld.com
// *****************************************************************************
#include <Memory.h>
#include <ToolUtils.h>
#include <Types.h>
#include <Icons.h>
#include <QuickDraw.h>
#include <Windows.h>
#include "SetupA4.h"
#include "A4Stuff.h"
#define ASSERT(cond) do{if(!cond) DebugStr("\p" #cond);}while(false)
#define VERIFY(cond) do{if(!cond) DebugStr("\p" #cond);}while(false)
#define OS_VERIFY(func) do{OSErr err = (func); ASSERT(err == noErr);}while(false)
const short kIconLocked = -835;
const short kIconUnlocked = -836;
const long kWindowUnlockedFlag = 0x8000;
static Boolean IsWindowLocked(WindowPeek window)
{
// Only work on windows with aux handles
AuxWinHandle awHndl = NULL;
if(GetAuxWin((GrafPtr)window, &awHndl))
{
return ((awHndl[0]->awFlags & kWindowUnlockedFlag) == 0);
}
return false;
}
static void SetWindowLock(WindowPeek window, Boolean locked)
{
// Only work on windows with aux handles
AuxWinHandle awHndl = NULL;
if(GetAuxWin((GrafPtr)window, &awHndl))
{
if(locked)
awHndl[0]->awFlags &= ~kWindowUnlockedFlag;
else
awHndl[0]->awFlags |= kWindowUnlockedFlag;
}
}
static Rect GetIconRect(WindowPeek window)
{
// Get the struct rect
Rect structRect = (**window->strucRgn).rgnBBox;
Rect result = { structRect.top + 2, structRect.right - 42, structRect.top + 18, structRect.right - 26 };
return result;
}
// —————————————————————————————————————————————————————————————————————————————
//
// SyncPorts
//
// —————————————————————————————————————————————————————————————————————————————
// Straight from the pages of _Macintosh Programming Secrets_, Second
// Edition by Scott Knaster and Keith Rollin (page 425). (except that this
// version doesn’t check Gestalt, it will only be called if CQD is running)
// This routines was added to 2.3. It makes sure the drawing environment
// is set correctly if the system has color. This is not needed for the
// code in this WDEF as it is, but if a DoWDrawGIcon handler is implemented,
// this is needed to make sure the drawing environment is set as Apple
// tells us it will be for drawing the gray, xor’ed border.
// —————————————————————————————————————————————————————————————————————————————
static void
SyncPorts()
{
GrafPtr bwPort;
CGrafPtr colorPort;
GetWMgrPort(&bwPort);
GetCWMgrPort(&colorPort);
SetPort((GrafPtr) colorPort);
BlockMoveData(&bwPort->pnLoc, &colorPort->pnLoc, 10);
BlockMoveData(&bwPort->pnVis, &colorPort->pnVis, 14);
PenPat((ConstPatternParam) &bwPort->pnPat);
BackPat((ConstPatternParam) &bwPort->bkPat);
}
static long
CallSystemWDEF0(short varCode, WindowPeek window, short message, long param)
{
static Handle hSystemWDEF0 = NULL;
if(!hSystemWDEF0)
{
// Load and lock down the original system WDEF 0
short refNum = CurResFile();
UseResFile ( 0 ); // system file
hSystemWDEF0 = Get1Resource ( 'WDEF', 0 );
UseResFile ( refNum );
HLock(hSystemWDEF0);
}
ASSERT(hSystemWDEF0);
ASSERT(*hSystemWDEF0);
return ((WindowDefProcPtr)(*hSystemWDEF0))(varCode, (GrafPort*)window, message, param);
}
static void DrawLockBox(WindowPeek window, Boolean selected)
{
// Calc the icon rect in top right
Rect iconRect = GetIconRect(window);
short iconId = IsWindowLocked(window) ? kIconLocked : kIconUnlocked;
IconTransformType trans = ttNone;
if(selected)
trans |= ttSelected;
// Plot the icons
OS_VERIFY(PlotIconID(&iconRect, atNone, trans, iconId));
}
static long
DoWindowHit(WindowPeek window, long param, long result)
{
Point hitPt;
hitPt.v = HiWord(param);
hitPt.h = LoWord(param);
switch(result)
{
case wInContent:
{
if(IsWindowLocked(window))
result = wInDrag; // let the user grab and drag the content region
break;
}
case wInDrag:
{
// Calc the icon rect in top right
Rect iconRect = GetIconRect(window);
if (PtInRect(hitPt, &iconRect))
{
if(Button())
{
CGrafPtr colorPort;
GetCWMgrPort(&colorPort);
RgnHandle rgn = ::NewRgn();
CopyRgn(colorPort->clipRgn, rgn);
ClipRect(&iconRect);
DrawLockBox(window, true);
while(Button())
/* nop - wait for mouse to be released */;
SetWindowLock(window, !IsWindowLocked(window));
DrawLockBox(window, false);
// leave the result as wInDrag
CopyRgn(rgn, colorPort->clipRgn);
DisposeRgn(rgn);
}
}
break;
}
}
return result;
}
// —————————————————————————————————————————————————————————————————————————————
//
// Windoid Main Function
//
// —————————————————————————————————————————————————————————————————————————————
// This is the main entry point for all calls to this code resource. It
// dispatches to routines that correspond to the message it is given.
// —————————————————————————————————————————————————————————————————————————————
pascal long
main(short varCode, WindowPeek window, short message, long param)
{
EnterCodeResource(); // Set up for globals
PrepareCallback();
long result = CallSystemWDEF0(varCode, window, message, param);
// If we don't have an aux window rec, ignore this window
AuxWinHandle awHndl = NULL;
if(GetAuxWin((GrafPtr)window, &awHndl))
{
// Don't put locks on dialog windows
if(window->windowKind != dialogKind)
{
GrafPtr savePort;
Boolean needSyncPorts;
// This sets up the appropriate drawing environment, but only for those
// messages for which we actually need to draw.
needSyncPorts = (message == wDraw
|| message == wHit /*
|| message == wGrow
|| message == wDrawGIcon*/);
if (needSyncPorts) {
GetPort(&savePort);
SyncPorts();
}
switch (message) {
case wNew:
case wDispose:
case wCalcRgns:
case wGrow:
case wDrawGIcon:
default:
break;
case wDraw:
DrawLockBox(window, false);
break;
case wHit:
// filter the result
result = DoWindowHit(window, param, result);
break;
}
if (needSyncPorts)
SetPort(savePort);
}
}
ExitCodeResource(); // Shutdown use of globals (A4).
return result;
}